/**
 * ATable.java
 *
 * Copyright (C) 2009,  Tomislav Milkovic
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package extjsdyntran.translation.store.db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public abstract class ATable implements ITable {

    protected PreparedStatement prstInsert;
	protected PreparedStatement prstUpdate;
	protected PreparedStatement prstDelete;
	protected PreparedStatement prstGetById;
	protected PreparedStatement prstSelectAll;
	protected PreparedStatement prstDeleteAll, prstGetByValue;
	
	final protected Logger loggerA = LoggerFactory.getLogger(ATable.class);
	
    public boolean sqlDelete(IRecord obj) {
        try {
            prstDelete.setInt(1, obj.getId());
            return DBaseFactory.statementExecute(prstDelete) > 0;
        }
        catch(SQLException e) {
        	loggerA.error("Error when deleting record",e);
        }
        return false;
    }
    
    public boolean sqlDeleteAll(IRecord obj) {
    	return DBaseFactory.statementExecute(prstDeleteAll) > 0;
    }
    

    // returns an ArrayList of all the elements in a table. All our objects implements GetSetId.
    public   ArrayList<IRecord> sqlSelectAll(PreparedStatement prstSelectAll) {
        ArrayList<IRecord> al = new ArrayList<IRecord>();
        ResultSet rs = DBaseFactory.queryExecute(prstSelectAll);
        try {

            while(rs.next()) {
            	IRecord obj = (IRecord) rs.getObject(1);   
                int id = rs.getInt(2);                   
                obj.setId(id);                           
                al.add(obj);                     
            }

            rs.close();
        }
        catch(SQLException e) {
        	loggerA.error("Error trying to retrieve records for {} ...",prstSelectAll,e);
        }

        return al;
    }

    
    /*
     * returns a row by its Id. The classes that call this method will cast the GetSetId object to
     * the object they served (FirstName, LastName or FullName)
     */
    public   IRecord sqlGetById(PreparedStatement prstGetById,int id) {
        try {
            prstGetById.setInt(1, id);
            ResultSet rs = DBaseFactory.queryExecute(prstGetById);
            if(rs == null)
                return null;
            if(!rs.next())
                return null;
            IRecord obj = (IRecord) rs.getObject(1);   
            obj.setId(id);
            return obj;
        }
        catch(SQLException e) {
        	loggerA.error("Error trying to retrieve records for {} ...",prstGetById,e);
        }
        return null;
    }
    
    /*
     * returns the row count from a table whose name is passed as parameter
     * returns -1 if the table does not exist
     */
    public  int getCount(String tableName) {
        int nb = -1;
        String sql = "SELECT COUNT(*) FROM " + tableName + ";";

        ResultSet rs = DBaseFactory.queryExecute(sql);

        if(rs == null) {
            System.out.println();
            loggerA.info("GetCount() on {} returns {}.",tableName, nb);
            return nb;
        }

        try {
            rs.next();
            nb = rs.getInt(1);
            rs.close();
        }
        catch (SQLException e) {
            throw new IllegalStateException(getClass().getName() + ".getCount() rs.getNext() or rs.getInt Exception: " + e);        
        }        
        loggerA.info("GetCount() on {} returns {}.",tableName, nb);
        return nb;
    }
}
